package com.qzboot.basic.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.qzboot.basic.service.mapper.SysRoleMapper;
import com.qzboot.common.constant.SysConst;
import com.qzboot.facade.entity.SysRole;
import com.qzboot.facade.entity.SysRoleMenu;
import com.qzboot.facade.service.SysRoleMenuService;
import com.qzboot.facade.service.SysRoleService;
import com.qzboot.facade.service.SysUserRoleService;
import com.qzboot.facade.support.CrudServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zengzhihong
 */
@Service
public class SysRoleServiceImpl extends CrudServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public List<SysRole> listByCreateBy(Long userId) {
        return baseMapper.selectList(Wrappers.<SysRole>lambdaQuery().eq(SysRole::getCreateByUserId, userId));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysRole save(SysRole entity) {
        baseMapper.insert(entity);
        if (CollectionUtil.isEmpty(entity.getMenuIdList())) {
            return entity;
        }
        return entity;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateById(SysRole entity) {
        //判断是否有权限编辑角色
        checkRole(entity.getId(), entity.getCreateByUserId());
        //保存角色
        return SqlHelper.retBool(baseMapper.updateById(entity));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeById(Long id, Long byId) {
        //判断是否有权限编辑角色
        checkRole(id, byId);
        //移除角色菜单
        sysRoleMenuService.removeByRoleId(id);
        //移除用户角色
        sysUserRoleService.removeByRoleId(id);
        //移除角色
        baseMapper.deleteById(id);
        return true;
    }

    @Override
    public boolean allocateRoleMenu(Long roleId, List<Long> menuIdList, Long byId) {
        //判断是否有权限编辑角色
        checkRole(roleId, byId);
        //移除角色菜单
        sysRoleMenuService.removeByRoleId(roleId);
        //保存菜单
        if (CollectionUtil.isNotEmpty(menuIdList)) {
            saveRoleMenu(roleId, menuIdList);
        }
        return true;
    }

    private void saveRoleMenu(Long roleId, List<Long> menuIdList) {
        List<SysRoleMenu> roleMenuList = CollectionUtil.newArrayList();
        for (Long menuId : menuIdList) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            roleMenuList.add(roleMenu);
        }
        sysRoleMenuService.saveBatch(roleMenuList);
    }

    private void checkRole(Long roleId, Long byId) {
        if (SysConst.ADMIN_USER_ID.equals(byId)) {
            return;
        }
        SysRole sysRole = this.getById(roleId);
        Assert.isTrue(ObjectUtil.isNotNull(sysRole) && sysRole.getCreateByUserId().equals(byId), "所选角色无权分配");
    }
}
